/**
 * Created by WangMing on 15/12/9.
 */
define(["../../../service",
        "../../../utils/i18n",
        "highcharts",
        "../../../widgets/qrcodewidget/qrcode_widget",
        "../../../widgets/player_live_widget/player_live_widget"
    ],
    function(api, i18n,Highcharts) {
        // 定义所有相关的vmodel
        var vm = avalon.define({
            $id: "albumPlay",
            $video: {
                method: api.getVideoPlay,
                videoST:{}

            },
            itemID: null,
            $player: {},
            $breadcrumb: {
                stateName: "albums.item.play",
                guid: "",
                title: ""
            },
            $setting:{},
            $videoTimeInfo: [],
            $videoSW: [],

            $skipArry: ["playerWidget", "itemID"],
            jwplayerSeek: function(second) {
                if (window.jw.seek) {
                    window.jw.seek(second);
                }
            },
            $init: function() {

            }
        });
        i18n.ngettext(" has been reviews", "%d people has been reviews", 2);
        vm.$watch("setting", function(param) {
            vm.$setting = avalon.define(
                avalon.mix(vm.$setting, param))

        })

        return avalon.controller(function($ctrl) {

            // 视图渲染后，意思是avalon.scan完成
            $ctrl.$onRendered = function() {
               // 保存专辑播放次数
               api.getItems({
                  method:api.setAlbumPv,
                  query:{
                      id:vm.itemID
                  }
              },function(res){
                  
              })
               // 视频播放次数
               api.getItems({
                  method:api.setVideoPv,
                  query:{
                      id:vm.vid
                  }
              },function(res){
                  
              })

                var StudentRatio = parseFloat(vm.$video.Video&&vm.$video.Video.studentRatio);
                var TeacherRatio = parseFloat(vm.$video.Video&&vm.$video.Video.teacherRatio);

                return new Promise(function(res,rej){
                    res()
                }).then(function(){
                    return Highcharts.chart('info_chart', {
                        credits: {
                            enabled: false
                        },
                        chart: {
                            type: 'pie',
                            backgroundColor: "transparent",
                            //spacingTop: 10,
                            marginTop: 0,
                            marginBottom: 0,
                            marginLeft: 10
                        },
                        plotOptions: {
                            pie: {
                                slicedOffset: 2,
                                dataLabels: {
                                    enabled: true
                                }
                            }
                        },
                        title: {
                            margin: 0,
                            floating: true,
                            text: "教学行为活动比例",
                            style: { fontSize: '14px', lineHeight: '18px', color: '#ffffff' },

                            marginBottom: 0,
                            verticalAlign: "middle"
                        },
                        xAxis: {
                            title: "比例"
                        },
                        series: [{
                            data: [{
                                    name: "老师活动比例",
                                    y: TeacherRatio||0,
                                    sliced: true,
                                    selected: true
                                },
                                ['学生活动比例', StudentRatio||0]
                            ],
                            step: 'right',
                            name: '比例'
                        }]
                    });
                    if(vm.$video.Video!=null)
                        Highcharts.chart() 
                    }).then(function(){
                        return Highcharts.chart('videost_chart',{
                            title: {
                                text: ''
                            },
                            credits: {
                                enabled: false
                            },
                            chart:{
                                zoomType:"xy",
                                backgroundColor:'#222222'
                            },
                            legend:{
                                enabled:false
                            },
                            plotOptions:{
                                line:{
                                    marker:{
                                        radius:2
                                    }
                                }
                            },
                            xAxis: {
                                min:0,
                                max: 60 * 60 * 1000,
                                tickInterval: 240 * 1000,
                                lineWidth: .5,
                                gridLineWidth: .5,
                                endOnTick: true,
                                tickWidth:0,
                                title: {
                                    text: "学生行为时间",
                                    align: "middle"
                                },
                                maxPadding: 0.1,
                                type: "datetime",
                                labels: {
                                     rotation: 0,
                                     x:-4,
                                    
                                    formatter: function () {
                                        //return Highcharts.dateFormat('%H:%M:%S', this.value);
                                        return this.value / 1000/6/10;
                                    }
                                }
                                // categories:[0,4,8,12,16,20,24,28,32,36,40,44,48,52,56,60]
                            },
                            yAxis: {
                                min: 0,
                                max: 60 * 60 * 1000,
                                tickInterval: 240 * 1000,
                                lineWidth: .5,
                                gridLineWidth: .5,
                                type: "datetime",
                                maxPadding: 0.1,
                                endOnTick :true,
                                title: {
                                    text: "教师行为时间",
                                    align: "middle",

                                },
                                labels: {
                                    formatter: function () {
                                        //return Highcharts.dateFormat('%H:%M:%S', this.value);
                                        return this.value / 1000/6/10;
                                    }
                                }
                                // categories:[0,4,8,12,16,20,24,28,32,36,40,44,48,52,56,60]

                            },
                           
                            tooltip: {
                                enable:false,
                                formatter: function () {
                           //         return '';
                                    return '' +
                                            '教师行为时间' + Highcharts.dateFormat('%H:%M:%S ', this.y) +
                                            '学生行为时间' + Highcharts.dateFormat('%H:%M:%S ', this.x);
                               }
                            },
                            series: [{
                                data:  vm.$video.videoST.data,
                                step: 'right',
                                type:'line',
                                pointInterval: 30 * 1000,
                                name: '行为曲线'
                            }]

                        });
                    })

            };
            // 进入视图
            $ctrl.$onEnter = function(param, rs, rj) {
                vm.itemID = param.itemID;
                vm.vid = param.vid;

                return new Promise(function(res, rej) {
                    res();
                }).then(function() {

                    return api.getItems({
                        method: api.getVideo,
                        query: {
                            id: vm.vid
                        }
                    }, function(res) {
                        if(res.success){
                            vm.$video.Video = {};
                            vm.$video.Video = _.merge(vm.$video.Video, res.data);
                            vm.$video.Video.size = (vm.$video.Video&&vm.$video.Video.length)||0
                            vm.$breadcrumb.title = vm.$video.Video.title;
                        }else{
                            vm.$video.Video = null;
                            vm.$error = res
                        }
                    })
                }).then(function() {
                    return api.getItems({
                        method: api.getVideoTimeInfo,
                        query: {
                            id: vm.vid

                        }
                    }, function(resInfo) {
                        vm.$video.VideoInfo = [];
                        if (!resInfo.success) {
                            vm.$video.VideoInfo = null
                            vm.$video.VideoInfo.error = true;
                        } else {
                            vm.$video.VideoInfo = _.merge(vm.$video.VideoInfo, resInfo.data)
                            vm.$video.VideoInfo.size = (vm.$video.VideoInfo&&vm.$video.VideoInfo.length)||0
                        }
                    })
                }).then(function() {
                    return api.getItems({
                        method: api.getVideoSW,
                        query: {
                            id: vm.vid
                        }
                    }, function(res) {
                         vm.$video.VideoSW = [];
                        if (!res.success) {
                            vm.$video.VideoSW = null
                            vm.$video.VideoSW.error = true;
                        } else {
                            vm.$video.VideoSW = _.merge(vm.$video.VideoSW, res.data);
                            vm.$video.VideoSW.size = (vm.$video.VideoSW&&vm.$video.VideoSW.length)||0
                        }
                    })
                }).then(function(){
                    // 行为分析
                    return api.getItems({
                        method:api.getVideoST,
                        query:{
                            id:vm.vid
                        }
                    },function(res){
                        console.log(res)
                        vm.$video.videoST.data = [];
                        if(!res.success){
                            vm.$video.videoST.error = true;
                        }else{
                            vm.$video.videoST = _.merge(vm.$video.videoST,res.data)
                            var s = 0,
                                t = 0;
                            for(var i=0;i<res.data.length;i++){
                                if(res.data[i].tag==1){
                                    s = res.data[i].seconds-t;
                                }else if(res.data[i].tag==0){
                                    t = res.data[i].seconds-s;
                                }
                                vm.$video.videoST.data.push([s*1000,t*1000])
                            }
                        }
                            vm.$video.videoST.size = vm.$video.videoST.data.length||0
                            console.log(vm.$video.videoST.size)
                        // console.log(vm.$video.videoST.data)

                    })
                }).then(function() {
                    return api.getItems({
                        method: api.getVideoPlay,
                        query: {
                            id: vm.$setting.vodVideoId||vm.vid
                        }

                    }, function(res) {
                        vm.$video.VideoResourcePath = avalon.define(
                            avalon.mix(vm.$video.VideoResourcePath,
                                avalon.mix({ $id: "VideoResourcePath" }, res)));
                        vm.$video.smil = "/json/videoplay.smil?id="+(vm.$setting.vodVideoId || vm.vid)+"&smil=1"
                        vm.$video.timeinfo = "/json/videoTimeInfo.action?id="+(vm.$setting.vodVideoId || vm.vid)+"&rss=1"
                        vm.$video.videoSW = "/json/videoSW.action?id="+(vm.$setting.vodVideoId || vm.vid)+"&rss=1";
                    }) 
                })

            };
            // 对应的视图销毁前
            $ctrl.$onBeforeUnload = function() {

            };
            $ctrl.$vmodels = [vm];
        })
    });
